using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class TwoFourPanel : MonoBehaviour
{
    public List<TwoFourItem> items = new List<TwoFourItem>();
    int[,] ints = new int[3, 2];
    public Transform ButGeoup;
    public Button button;
    public Image MainImage;
    bool isGet = false;
    public Text Numtext;
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < ints.GetLength(0); i++)
        {
            for (int j = 0; j < ints.GetLength(1); j++)
            {
                ints[i, j] = 1;
                int ii = i;
                int jj = j;
                var but = Instantiate(button, ButGeoup);
                but.GetComponent<RectTransform>().localPosition = new Vector3(i * 200, j * 200, 0);
                but.onClick.AddListener(() =>
                {
                    if (TwoPanel.Ins.MoveNum > 0)
                    {
                        ints[ii, jj] = 0;
                        refreshX(ii);
                        refreshY(jj);
                        Destroy(but.gameObject);
                        TwoPanel.Ins.MoveNum--;
                        Numtext.text = "旅行券：" + TwoPanel.Ins.MoveNum;
                    }
                });
            }
        }
        button.gameObject.SetActive(false);
        Destroy(ButGeoup.GetComponent<GridLayoutGroup>());
        Numtext.text = "旅行券：" + TwoPanel.Ins.MoveNum;
    }
    void OnEnable()
    {
        if (TwoPanel.Ins)
        {

            Numtext.text = "旅行券：" + TwoPanel.Ins.MoveNum;
        }
    }
    public void MainBut()
    {
        var item = items.FirstOrDefault(a => a.state == 0);

        if (item != null) return;

        Debug.Log("已经领取过了");

        if (isGet) return;

        TwoPanel.Ins.MoneyNum += 100;
        MainImage.gameObject.SetActive(true);
        isGet = true;
        Debug.Log("领取过了成功");
    }

    private void refreshY(int jj)
    {
        for (int i = 0; i < ints.GetLength(0); i++)
        {
            if (ints[i, jj] == 1) return;
        }
        var item = items.FirstOrDefault(a => a.type == TwoFourType.横向 && a._index == jj);

        item.state = 1;
    }

    private void refreshX(int ii)
    {
        for (int i = 0; i < ints.GetLength(1); i++)
        {
            if (ints[ii, i] == 1) return;
        }
        var item = items.FirstOrDefault(a => a.type == TwoFourType.纵向 && a._index == ii);

        item.state = 1;
    }
    // Update is called once per frame
    void Update()
    {

    }
}
